/* 
 * Copyright (c)	Insighten Intelligent Equipment co.,LTD
 * 
 *  Use of this software is controlled by the terms and conditions found
 *  in the license agreement under which this software has been supplied.
 * 
 */
/** 
 * @file msg_thread.c
 * @brief message process thread 
 * @author taco [tacoee@gmail.com]
 * @version 0.1
 * @date 2010-09-13
 */
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <pthread.h>

#include "types.h"

typedef struct _MsgProc {
	pthread_mutex_t	mutex;
	pthread_cond_t	cond;
	char *msg;
	unsigned int msg_len;
}MsgProc;

MsgProc *msgproc_create()
{
	MsgProc *thiz = NULL;

	thiz = calloc(sizeof(MsgProc), 1);

	if(thiz != NULL) {
		pthread_mutex_init(&thiz->mutex, NULL);
		pthread_cond_init(&thiz->cond, NULL);
	}

	return thiz;
}

Ret msgproc_destory(MsgProc *thiz)
{
	if (thiz != NULL) {
		pthread_mutex_destroy(&thiz->mutex);
		pthread_cond_destroy(&thiz->cond);
		thiz->msg = NULL;
		free(thiz);
		return RET_OK;
	}

	return RET_FAIL;
}

Ret msgproc_message_set(MsgProc *thiz, char *msg)
{
	return_val_if_fail(thiz !=NULL && msg != NULL, RET_FAIL);
	
	pthread_mutex_lock(&thiz->mutex);
	thiz->msg = msg;
	pthread_mutex_unlock(&thiz->mutex);

	return RET_OK;
}
Ret msgproc_message_clear(MsgProc *thiz)
{
	return_val_if_fail(thiz != NULL && thiz->msg != NULL, RET_FAIL);
	
	pthread_mutex_lock(&thiz->mutex);
	memset(thiz->msg, 0, thiz->msg_len);
	pthread_mutex_unlock(&thiz->mutex);

	return RET_OK;
}
char *msgproc_message_wait(MsgProc *thiz)
{
	return_val_if_fail(thiz != NULL, NULL);

	pthread_cond_wait(&thiz->cond, &thiz->mutex);

	return thiz->msg;
}

Ret msgproc_message_send(MsgProc *thiz)
{
	return_val_if_fail(thiz != NULL, RET_FAIL);

	pthread_cond_signal(&thiz->cond);

	return RET_OK;
}

void *msg_thread(void *arg)
{

}
